/*
 * @lc app=leetcode.cn id=107 lang=javascript
 *
 * [107] 二叉树的层序遍历 II
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrderBottom = function (root) {
  const res = []
  const queue = []
  queue.push(root)
  if (root === null) {
    return res
  }
  while (queue.length!==0) {
    let len = queue.length;
    const curLevel=[]
    for (let i = 0; i < len; i++){
      const cur= queue.shift()
      curLevel.push(cur.val)
      cur.left&&(queue.push(cur.left))
      cur.right&&(queue.push(cur.right))
    }
    res.push(curLevel)
  }
  return res.reverse()
};
// @lc code=end

